package com.aube.interact.fighter.model;

import com.huyn.baseframework.utils.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

/**
 * Created by huyaonan on 15/11/7.
 */
public class TimeLinePiece implements Serializable {

    public static final String TYPE_QA = "QA";
    public static final String TYPE_VOTE = "VOTE";
    public static final String TYPE_VS = "VS";
    public static final String TYPE_QUIZ = "QUIZ";
    public static final String TYPE_INFO = "INFO";
    public static final String TYPE_VIDEO= "MULTICAMERA";

    public String type="";
    public String rid="";

    public String thumbnailCard="N";

    /**
     * 多镜头预载时间
     */
    public int preloadSeconds=0;

    public int startSeconds=0;
    public int endSeconds=0;
    public String title="";
    public String tlDesc="";//多镜头的红字

    public TimeLineItemDetail detail;

    //视频的播放时间
    public long playTime=0;

    //卡片已经开始了多长时间
    public long timePassed = 0;

    /**卡片的位置以及动画相关配置*/
    public CardStyleModel config;

    public boolean showInfoDetail = true;

    /**是否显示竞猜的结果,如果没有选过,就不显示*/
    public boolean showQuizResult = false;

    public boolean isIllegal = true;

    public String getOptionImgUrl(int index) {
        return detail.getOptionImgUrl(index);
    }

    public int preLoadVideo = -1;

    public boolean hasImgInOption() {
        if(detail != null)
            return detail.hasImgInOption();
        return false;
    }

    public int getRightAnswerIndex() {
        if(detail != null)
            return detail.getRightAnswerIndex();
        return -1;
    }

    public int getRelativePosition() {
        if(config == null)
            return 5;
        return config.getRelativePosition();
    }

    public String getAnimEffects() {
        if(config == null)
            return "";
        return config.animationEffects;
    }

    public float[] getCardIndex() {
        float[] result = new float[2];
        if(config == null)
            return result;
        return config.getCardIndex();
    }

    public void addToPicMap(HashMap<Integer, String> maps) {
        if(detail != null)
            detail.addToPicMap(maps, startSeconds);
    }

    public int getTimeDown() {
        return detail == null ? 0 : detail.timedown;
    }

    public boolean isBlank() {
        return StringUtils.isBlank(type);
    }

    public int getPeriod() {
        int left = endSeconds - startSeconds;
        if(left < timePassed)
            return 0;
        return (int) (left - timePassed);
    }

    public String getNote() {
        String result = detail == null ? "" : detail.cardnote;
        if(StringUtils.isBlank(result))
            return "";
        return result+"，";
    }

    public String getImgUrl() {
        if(detail == null)
            return "";
        return detail.getImgUrl();
    }

    public String getImgTag() {
        if(detail == null)
            return "";
        return detail.getImgTag();
    }

    public List<OptionItem> getOptions() {
        if(detail == null || detail.optionList == null)
            return new ArrayList<>();
        return detail.optionList;
    }

    public int optionSize() {
        return getOptions().size();
    }

    public int getCountDownTime() {
        if(TYPE_QA.equalsIgnoreCase(type)) {
            if(detail.revealTheAnswer > startSeconds+timePassed)
                return (int) (detail.revealTheAnswer - startSeconds-timePassed);
        }
        return 0;
    }

    public boolean isVideoCard() {
        return TYPE_VIDEO.equalsIgnoreCase(type);
    }

    public String getOptionId(int index) {
        if(detail != null)
            return detail.getOptionId(index);
        return null;
    }

    public String getOptionValue(int index) {
        if(detail != null)
            return detail.getOptionValue(index);
        return "";
    }

    public int getOptionIndex(String optionId) {
        if(detail != null)
            return detail.getOptionIndex(optionId);
        return -1;
    }

    public boolean isCardFold() {
        return "Y".equalsIgnoreCase(thumbnailCard);
    }

    public List<TimeLinePiece> format() {
        List<TimeLinePiece> list = new ArrayList<>();
        if(TYPE_QUIZ.equalsIgnoreCase(type)) {
            list.add(this);
            TimeLinePiece result = getResultData();
            if(result != null)
                list.add(result);
        } else {
            list.add(this);
        }
        return list;
    }

    private TimeLinePiece getResultData() {
        try {
            TimeLinePiece result = new TimeLinePiece();

            result.type = type;
            result.title = title;
            result.tlDesc = tlDesc;
            result.config = config;
            result.detail = detail;

            QuizResult quizResult = result.detail.timelineQuizInfoRespVo;
            result.startSeconds = quizResult.getStartSeconds();
            result.endSeconds = quizResult.getEndSeconds();
            result.rid = rid + "_" + type;
            result.showQuizResult = true;

            if(endSeconds > result.startSeconds)
                return null;

            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public String getRid() {
        if(rid.contains(type))
            return rid.substring(0, rid.indexOf("_"));
        return rid;
    }

}
